#include "QClientInit.h"
#include "../QCommon/QCommon.h"
#include <string.h>
#include <string>
#include "QClientInterface.h"
#include <iostream>
#include "Command.h"
#include <stdlib.h>
#define LIST    "l"
#define SEND_M  "m"
#define SEND_F  "f"
#define QUIT    "q"
#define HELP	"h"

using namespace std;
int main(int argc,char **argv)
{
    if(argc!=2)
    {
        cout<<"Usage: LanQClient yourname"<<endl;
        return 0;
    }

        start();
        User *u=new User;
        u->name=argv[1];
        setUser(*u);
        int on=true;
        char buf[100];
	cout << "Lan-Q\tCommunicate as you wish!" << endl;
	cout << "Type h for help" << endl;
        while(on)
        {
            cin>>buf;
            if(strcmp(buf,LIST)==0)
            {
                //TODO list
                list();
            }
            else if(strcmp(buf,SEND_M)==0)
            {
                //TODO send_m
                int id;
                cin >> id;
                char* message=new char[512];
                cin.getline(message, 512);
                send_message(message,id);
                delete[] message;
		        cin.clear();
            }
            else if(strcmp(buf,SEND_F)==0)
            {
                //TODO send_f
                //char* ip=new char[128];
                //cin >> ip;
                int id;
                cin >> id;
                char* file_path=new char[512];
                cin.getline(file_path,512);
                send_file(file_path,id);
                delete[] file_path;
	        	cin.clear();
            }
        	else if(strcmp(buf,HELP)==0)
	        {
        		cout << "***************** Help Message *****************" << endl;
		        cout << "command form: command + args" << endl;
		        cout << "command\targs" << endl;
		        cout << "m\t<id><message>\t" << "Descripton: send message.\tExample: m 0 hello world!" << endl;
		        cout << "l\t\t\t" << "Description: list all the users." << endl;
		        cout << "f\t<id><file_url>\t" << "Description: send file.\t Example: f 1 /home/main.cpp" << endl;
		        cout << "q\t\t\t" << "Description: quit." << endl;
	        	cout << "h\t\t\t" << "Description: get help information." << endl;
        		cout << "***********************************************" << endl;
	        	cout << "< type your command ... use 'h' for help>" << endl;
        	}
            else if(strcmp(buf,QUIT)==0)
            {
                exit(0);
            }
            else
            {
                int fd=open(Q_SYN,O_RDWR);
                if(fd>0)
                {
                    if(buf[0]=='y'||buf[0]=='Y')
                    {
                        cout<<"Store to: ";
                        cin>>buf;
                        write(fd,buf,strlen(buf)+1);
                        close(fd);
                    }
                    else
                    {
                        buf[0]='#';
                        write(fd,buf,1);
                        close(fd);
                    }
                }
            }
        }
}
